//
//  GLGWBaseViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/8.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "GLGWBaseViewController.h"


@interface GLGWBaseViewController ()
@property (nonatomic,strong) UIView* noDataView;

@end

@implementation GLGWBaseViewController

- (UIStatusBarStyle)preferredStatusBarStyle{
    return _StatusBarStyle;
}
//动态更新状态栏颜色
-(void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle{
    _StatusBarStyle=StatusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
    self.navigationController.navigationBar.translucent = NO;
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[YLJ_ImageTool imageWithLineColor:[UIColor colorWithHexString:@"#F2F2F2"]]];//导航条下面的线
    self.isShowLiftBack = YES;
    self.StatusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    kKeyboardEndEditing
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

-(void)popViewControllerAction{
    [self.navigationController  popViewControllerAnimated:YES];
}

-(void)goLoginWithPush{
    //    [self.navigationController pushViewController:[LoginViewController new] animated:YES];
}

-(void)noall{
    
}

- (void)backBtnClicked
{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/**
 *  是否显示返回按钮
 */
- (void) setIsShowLiftBack:(BOOL)isShowLiftBack
{
    _isShowLiftBack = isShowLiftBack;
    self.controllerCount = self.navigationController.viewControllers.count;
    DLog(@"------------------控制器的数量---------------%ld",(long)self.controllerCount);
    //下面判断的意义是 当VC所在的导航控制器中的VC个数大于1 或者 是present出来的VC时，才展示返回按钮，其他情况不展示
    if (self.controllerCount<2) {
        self.navigationItem.hidesBackButton = YES;
        UIBarButtonItem * NULLBar=[[UIBarButtonItem alloc]initWithCustomView:[UIView new]];
        self.navigationItem.leftBarButtonItem = NULLBar;
    }
}

-(UIBarButtonItem *)rt_customBackItemWithTarget:(id)target action:(SEL)action{
    if (_isShowLiftBack) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 40, 40);
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        return [[UIBarButtonItem alloc] initWithCustomView:btn];
    }else{
        return nil;
    }
}

-(void)noAllPBack{
    NSMutableArray * items = [[NSMutableArray alloc] init];
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(0, 0, 30, 20);
    [btn sizeToFit];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [items addObject:item];
    self.navigationItem.leftBarButtonItems = items;
}

- (void)addNavigationItemWithImageNames:(NSArray *)imageNames isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
    NSMutableArray * items = [[NSMutableArray alloc] init];
    NSInteger i = 0;
    for (NSString * imageName in imageNames) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 40, 40);
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        btn.tag = [tags[i++] integerValue];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];

    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
}

- (void)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags theButTitColor:(UIColor *)t_Color titFont:(nonnull UIFont *)a_font
{

    NSMutableArray * items = [[NSMutableArray alloc] init];

    //调整按钮位置
    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width= -10;
    [items addObject:spaceItem];

    NSInteger i = 0;
    for (NSString * title in titles) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(0, 0, 30, 20);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = a_font;
        [btn setTitleColor:t_Color forState:UIControlStateNormal];
        btn.tag = [tags[i++] integerValue];
        [btn sizeToFit];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];
    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
}

- (void)dealloc{
    DLog(@"我消失啦~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~😆😆");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -  屏幕旋转
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //当前支持的旋转类型
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    // 是否支持旋转
    return NO;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    // 默认进去类型
    return   UIInterfaceOrientationPortrait;
}

//懒加载
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64) style:UITableViewStylePlain];
        //头部刷新 88 83
        if (iPhoneX) {
            _tableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 88 - kHeightProportion_In_iPhoneTriploid(34));//88 + 83
        }
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
        header.automaticallyChangeAlpha = YES;
        header.lastUpdatedTimeLabel.hidden = NO;
        //        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = header;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        //底部刷新
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

-(UITableView *)groupedTableView{
    if (_groupedTableView == nil) {
        _groupedTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64) style:UITableViewStyleGrouped];
        //头部刷新 88 83
        if (iPhoneX) {
            _groupedTableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 88 - kHeightProportion_In_iPhoneTriploid(34));//88 + 83
        }
        _groupedTableView.backgroundColor = [UIColor whiteColor];
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
        header.automaticallyChangeAlpha = YES;
        header.lastUpdatedTimeLabel.hidden = NO;
        //        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _groupedTableView.showsVerticalScrollIndicator = NO;
        _groupedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _groupedTableView.mj_header = header;
//        _groupedTableView.estimatedRowHeight = 0;
//        _groupedTableView.estimatedSectionHeaderHeight = 0;
//        _groupedTableView.estimatedSectionFooterHeight = 0;
        _groupedTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        //底部刷新
        _groupedTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
        _groupedTableView.scrollsToTop = YES;
    }
    return _groupedTableView;
}

/**
 *  懒加载collectionView
 *
 *  @return collectionView
 */
- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        _flow = [[YLJCollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth , KScreenHeight-64) collectionViewLayout:_flow];
        if (iPhoneX) {
            _collectionView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 88 - kHeightProportion_In_iPhoneTriploid(34));//88 + 83
        }
        _collectionView.backgroundColor = [UIColor whiteColor];

        _collectionView.scrollsToTop = YES;
    }
    return _collectionView;
}

-(void)headerRereshing{
    
}

-(void)footerRereshing{
    
}

-(UIScrollView *)myScrollView{
    if (!_myScrollView) {
        _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64)];
        if (iPhoneX) {
            _myScrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 88- kHeightProportion_In_iPhoneTriploid(34));
        }
        _myScrollView.backgroundColor = [UIColor whiteColor];
        //滚动视图和导航控制器共同使用 xcode5.0以后编译器会自动设置滚动视图的显示位置
        self.automaticallyAdjustsScrollViewInsets = NO;
        //设置滚动视图内容视图的大小
        _myScrollView.contentSize = CGSizeMake(_myScrollView.width, _myScrollView.height);
        //设置禁止边框回弹效果
        _myScrollView.bounces = NO;
        //隐藏横纵滚动条
        _myScrollView.showsHorizontalScrollIndicator = NO;
        _myScrollView.showsVerticalScrollIndicator = NO;
    }
    return _myScrollView;
}

@end
